﻿/// <summary>
/// Developer: CHOOI KAH WAI
/// Last Modified: 20/10/2009
/// </summary>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;

namespace ARSS.Util
{
    /// <summary>
    /// Matrix Utility
    /// </summary>
    public static class MatrixUtil
    {
        #region Functions

        /// <summary>
        /// Convert matrix from double[] to WPF Matrix3D
        /// </summary>
        /// <param name="source">Source matrix</param>
        /// <returns>WPF Matrix3D matrix</returns>
        public static Matrix3D GetWPFMatrix(double[] source)
        {
            Matrix3D dest = new Matrix3D();
            dest.M11 = source[0];
            dest.M12 = source[1];
            dest.M13 = source[2];
            dest.M14 = source[3];
            dest.M21 = source[4];
            dest.M22 = source[5];
            dest.M23 = source[6];
            dest.M24 = source[7];
            dest.M31 = source[8];
            dest.M32 = source[9];
            dest.M33 = source[10];
            dest.M34 = source[11];
            dest.OffsetX = source[12];
            dest.OffsetY = source[13];
            dest.OffsetZ = source[14];
            dest.M44 = source[15];

            return dest;
        }

        /// <summary>
        /// Convert matrix from double[] to WPF Matrix3D
        /// </summary>
        /// <param name="source">Source matrix</param>
        /// <returns>WPF Matrix3D matrix</returns>
        public static double[] GetMatrixFromWPF(Matrix3D source)
        {
            double[] dest = new double[16];

            dest[0] = source.M11;
            dest[1] = source.M12;
            dest[2] = source.M13;
            dest[3] = source.M14;
            dest[4] = source.M21;
            dest[5] = source.M22;
            dest[6] = source.M23;
            dest[7] = source.M24;
            dest[8] = source.M31;
            dest[9] = source.M32;
            dest[10] = source.M33;
            dest[11] = source.M34;
            dest[12] = source.OffsetX;
            dest[13] = source.OffsetY;
            dest[14] = source.OffsetZ;
            dest[15] = source.M44;

            return dest;
        }

        /// <summary>
        /// Convert matrix from openGL float[] format to WPF double[] format
        /// From Column-major to Row-Major
        /// </summary>
        /// <param name="source">Source matrix</param>
        /// <returns>WPF double[] matrix</returns>
        public static double[] GetDMatrixFromOGLM(float[] source)
        {
            double[] dest = new double[16];

            dest[0] = source[0];
            dest[1] = source[4];
            dest[2] = source[8];
            dest[3] = 0;
            dest[4] = source[1];
            dest[5] = source[5];
            dest[6] = source[9];
            dest[7] = 0;
            dest[8] = source[2];
            dest[9] = source[6];
            dest[10] = source[10];
            dest[11] = 0;
            dest[12] = source[3];
            dest[13] = source[7];
            dest[14] = source[11];
            dest[15] = 1;

            return dest;
        }

        /// <summary>
        /// Convert matrix from NyARTransMat float[] format to WPF double[] format
        /// From Column-major to Row-Major
        /// </summary>
        /// <param name="source">Source matrix</param>
        /// <returns>WPF double[] matrix</returns>
        public static double[] GetDMatrixFromNyARTransMat(double[] source)
        {
            double[] dest = new double[16];

            dest[0] = source[0];
            dest[1] = source[4];
            dest[2] = -source[8];
            dest[3] = 0;
            dest[4] = source[1];
            dest[5] = source[5];
            dest[6] = -source[9];
            dest[7] = 0;
            dest[8] = source[2];
            dest[9] = source[6];
            dest[10] = -source[10];
            dest[11] = 0;
            dest[12] = source[3];
            dest[13] = source[7];
            dest[14] = -source[11];
            dest[15] = 1;

            return dest;
        }

        #endregion
    }
}
